package team.seekers.hula.user.school.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.entity.result.ApiException;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.teamwork.team.teamUser.service.TeamUserService;
import team.seekers.hula.teamwork.todo.service.TodoService;
import team.seekers.hula.user.school.entity.dao.SchoolDO;
import team.seekers.hula.user.school.entity.dto.SchoolAddDTO;
import team.seekers.hula.user.school.entity.dto.SchoolQueryDTO;
import team.seekers.hula.user.school.entity.dto.SchoolUpdateDTO;
import team.seekers.hula.user.school.entity.vo.SchoolInfoVO;
import team.seekers.hula.user.school.entity.vo.SchoolListVO;
import team.seekers.hula.user.school.mapper.SchoolMapper;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 一个学校(或者说一个课程班) 服务实现类
 *
 * @author yiming
 */
@Service
public class SchoolService extends ServiceImpl<SchoolMapper, SchoolDO>{

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    private TeamUserService teamUserService;

    @Autowired
    @Lazy
    private TodoService todoService;

    /**
     * 根据id获取学校信息
     */
    public SchoolInfoVO get(String id) {
        SchoolDO team = getById(id);
        if(team == null){
            throw new ApiException(ApiExceptionEnum.COMMON_FAIL, "学校已经解散");
        }

        SchoolInfoVO teamInfoVO = new SchoolInfoVO();
        // 基本信息赋值
        BeanUtils.copyProperties(team, teamInfoVO);

        //TODO 学校成员数

        return teamInfoVO;
    }

    /**
     * 新建学校
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(SchoolAddDTO teamAddPA) {
        String userId = MindUtils.getUserId();
        SchoolDO team = new SchoolDO();

        BeanUtils.copyProperties(teamAddPA, team);
        team.setUserId(userId);
        save(team);

        //TODO 将创建者加入学校
    }

    /**
     * 编辑学校
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(SchoolUpdateDTO schoolUpdateDTO) {
        SchoolDO schoolDO = getById(schoolUpdateDTO.getId());
        if(schoolDO == null){
            throw new ApiException(ApiExceptionEnum.COMMON_FAIL, "学校已经解散");
        }
        BeanUtils.copyProperties(schoolUpdateDTO, schoolDO);
        updateById(schoolDO);
    }

    /**
     * 删除学校
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String teamId) {
        // TODO 根据学校id删除学校团队待办

        // TODO 根据学校id解绑所有用户中记录

        // 删除学校
        removeById(teamId);
    }

    /**
     * 加入学校
     */

    @Transactional(rollbackFor = Exception.class)
    public void joinIn(String id) {
        SchoolDO schoolDO = getById(id);
        // TODO 判断是否已经加入过了


        // TODO 加入学校
        userService.setSchoolId(id,MindUtils.getUserId());
        // TODO 创建加入学校之后的默认待办
    }

    /**
     * 退出学校
     */
    @Transactional(rollbackFor = Exception.class)
    public void out(String teamId) {


        SchoolDO schoolDO = getById(teamId);
        String userId = MindUtils.getUserId();

        // 修改学校绑定状态

        // 减少学校绑定人数

        // 删除所在学校团队的待办

    }

    /**
     * 根据学校名分页查询
     */
    public PageResult<SchoolListVO> pageQuery(SchoolQueryDTO schoolQueryDTO) {
        Page<SchoolDO> teamPage = new Page<>(schoolQueryDTO.getPageNow(), schoolQueryDTO.getPageSize());
        QueryWrapper<SchoolDO> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(schoolQueryDTO.getTitle())) {
            wrapper.eq("title", schoolQueryDTO.getTitle());
        }
        this.page(teamPage, wrapper);
        List<SchoolDO> teams = teamPage.getRecords();
        long total = teamPage.getTotal();
        if(teams == null){
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
        // 将team集合内容拷贝至teamListVO集合
        List<SchoolListVO> teamListVOS = new ArrayList<>();
        SchoolListVO teamListVO;

        for (SchoolDO team : teams) {
            teamListVO = new SchoolListVO();
            BeanUtils.copyProperties(team, teamListVO);
            teamListVOS.add(teamListVO);
        }

        return new PageResult<>(total,teamListVOS);
    }

}
